﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Emoney.UserTraining.BLL;
using Emoney.UserTraining.Model;

namespace Emoney.UserTraining.Admin.aspx
{
    public partial class AddLecturer : System.Web.UI.Page
    {
        Traning_LecturerBLL bll = new Traning_LecturerBLL();
        Traning_Lecturer model = new Traning_Lecturer();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string id = Request["id"];
                if (!string.IsNullOrEmpty(id) && id != "0")
                {
                    model = bll.GetModel(Convert.ToInt32(id));
                    if (model != null)
                    {
                        hid_id.Value = model.id.ToString();
                        lecturerDetail.Value = model.lecturerDetail;
                        img_lecturerImage.Src = model.lecturerImage;
                        lecturerName.Value = model.lecturerName;
                        lecturerType.Value = model.lecturerType;
                    }
                }
            }
        }

        protected void btn_fileLecturerImage_Click(object sender, EventArgs e)
        {
            #region   //图片存储
            if (fileLecturerImage.PostedFile.FileName == "")
            {
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('请选择图片');", true);
                //Label1.Text = "您还没有选择图片！";
                return;
            }
            else
            {
                HttpPostedFile hp = fileLecturerImage.PostedFile;//创建访问客户端上传文件的对象
                string filepath = fileLecturerImage.PostedFile.FileName;//取得路径
                int uplength = fileLecturerImage.PostedFile.ContentLength;//图片大小
                string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);//文件名称带后缀名
                string name = filename.Substring(0, filename.Length - 4);//文件名不带后缀名
                string fileEx = filepath.Substring(filepath.LastIndexOf(".") + 1);//后缀名
                //判断图片格式
                if (fileEx == "jpg" || fileEx == "bmp" || fileEx == "gif")
                {
                    Stream sr = hp.InputStream;//创建数据流对象
                    byte[] b = new byte[uplength];//定义byte型数组
                    sr.Read(b, 0, uplength);//将图片数据放到b数组对象实例中，其中0代表数组指针的起始位置，uplength表示要读取流的长度（指针的结束位置）
                    try
                    {
                        Comm.HttpUploadFile fileUp = new Comm.HttpUploadFile();
                        string fileuploadUrl = Comm.Config.FileuploadUrl;
                        string moduleName = Comm.Config.ModuleName;
                        string result = fileUp.UploadFile(fileuploadUrl, moduleName, DateTime.Now.ToString("yyyyMMddHHmmss") + filename, b);
                        if (!string.IsNullOrEmpty(result))
                        {
                            UploadResult upRe =Comm.JsonTools.ToObject<UploadResult>(result);
                            if (upRe != null)
                                img_lecturerImage.Src = upRe.url;
                        }
                    }
                    catch (Exception ex)
                    {
                        //Label2.Text = "添加失败";
                        ClientScript.RegisterStartupScript(GetType(), "aa", "alert('上传失败');", true);
                    }
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "aa", "alert('上传图片不正确');", true);
                    //Label1.Text = "上传图片扩展名错误";
                }
            }
            #endregion
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(hid_id.Value) && hid_id.Value != "0")
            {
                model.id = Convert.ToInt32(hid_id.Value);
            }
            model.lecturerDetail = lecturerDetail.Value;
            if (!string.IsNullOrEmpty(img_lecturerImage.Src) && img_lecturerImage.Src != "~/images/lecturerImage0.jpg")
            {
                model.lecturerImage = img_lecturerImage.Src;
            }
            model.lecturerName = lecturerName.Value;
            model.lecturerType = lecturerType.Value;
            if (bll.AddUpdate(model) > 0)
            {
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('保存成功');ReturnPage();", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('保存失败');", true);
            }
        }
    }
}